package fu.abc.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fu.abc.DAO.ErrorDAO;
import fu.abc.model.Error;

/**
 * Servlet implementation class ReportError
 */
@WebServlet("/ReportError")
public class ReportError extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReportError() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		int location = Integer.parseInt(request.getParameter("id"));
		String errorname = request.getParameter("errorname");
		String content = request.getParameter("contentError");
		
		int status = 1;
		
		String message = "Bạn đã gửi thành công";
		
		Error err = new Error();
		ErrorDAO errdao = new ErrorDAO();
		
		err.setLocation(location);
		err.setErrorName(errorname);
		err.setContent(content);
		err.setStatus(status);
		
		try {
			if(errdao.addError(err)==true){
				request.setAttribute("mess", message);
				RequestDispatcher dis = getServletContext().getRequestDispatcher("/end-user/reporterror.jsp");
				dis.forward(request, response);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
